import turtle
import math

turtle.setup(500,500,0.1,0.1)

calc_distance = lambda p1, p2: math.sqrt(math.pow(p1[0]-p2[0],2) + math.pow(p1[1]-p2[1],2))

def draw_line(begin_point, distance, angle, gap=0):
    turtle.penup()
    turtle.setpos(begin_point)
    turtle.pendown()
    turtle.seth(angle)
    move_cnt = 3
    if gap > 0:
        for i in range(0, int(distance / gap)):
            if i % 2 == 0:
                turtle.pendown()
                move_cnt += 1
            else: 
                turtle.penup()
                move_cnt += 1
            turtle.forward(gap)
            move_cnt += 1
    
    elif gap == 0:
        turtle.forward(distance)
        move_cnt += 1
    
    return move_cnt


p1 = (10,10)
p2 = (100,100)
p3 = (40,60)
p4 = (70,90)

rect1 = ((-200,-100), (50, 50))
rect2 = ((-150,-50), (100, 100))

r1br = (rect1[1][0], rect1[0][1])
r1tl = (rect1[0][0], rect1[1][1])

r2br = (rect2[1][0], rect2[0][1])
r2tl = (rect2[0][0], rect2[1][1])

turtle.speed(10)

s = 0

s += draw_line(rect1[0],    calc_distance(rect1[0], r1br), 0)
s += draw_line(r1br,        calc_distance(r1br, rect1[1]), 90)
s += draw_line(rect1[1],    calc_distance(rect1[1], r1tl), 180)
s += draw_line(r1tl,        calc_distance(r1tl, rect1[0]), 270)

s += draw_line(rect2[0],    calc_distance(rect2[0], r2br), 0, 5)
s += draw_line(r2br,        calc_distance(r2br, rect2[1]), 90)
s += draw_line(rect2[1],    calc_distance(rect2[1], r2tl), 180)
s += draw_line(r2tl,        calc_distance(r2tl, rect2[0]), 270, 5)

s += draw_line(rect1[0],    calc_distance(rect1[0], rect2[0]), 45, 5)
s += draw_line(r1tl,        calc_distance(r1tl, r2tl), 45)
s += draw_line(r1br,        calc_distance(r1br, r2br), 45)
s += draw_line(rect1[1],    calc_distance(rect1[1], rect2[1]), 45)

while s > 0: 
    turtle.undo()
    s -= 1

turtle.done()
